﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Sector7.Restaurant.CoreType.Enums;
using Sector7.Restaurant.Modules.REST.Utility;
using Sector7.Restaurant.Modules.REST.LocalService;
using Sector7.Framework.Enums;
using Sector7.Framework.Utility;
using Sector7.Framework.Core;

namespace Sector7.Restaurant.Modules.REST.Windows
{
    /// <summary>
    /// AddTableWindow.xaml 的交互逻辑
    /// </summary>
    public partial class AddTableWindow : AddModeWindowBase
    {
        public AddTableWindow(RS_Room selectedRoom, List<RS_Room> rooms)
        {
            InitializeComponent();

            cbbRoom.ItemsSource = rooms;
            cbbRoom.SelectedItem = selectedRoom;
        }

        public RS_Table Table
        {
            get { return m_entity as RS_Table; }
            set { m_entity = value; }
        }

        protected override void InitializeForCreating()
        {
            Table = new RS_Table();
            Table.Status = (int)TableStatusType.Idle;
        }

        protected override void InitializeForUpdating()
        {
            btnContinue.Visibility = Visibility.Hidden;
        }

        protected override void AddData()
        {
            Table.RoomId = (cbbRoom.SelectedItem as RS_Room).RoomId;

            switch (m_crud)
            {
                case CrudType.Create:
                    Table.TableId = Aide.LocalClient.Invoke<int>(
                        service => service.AddTable(Table));
                    break;
                case CrudType.Update:
                    EntityDelta delta = ClientUtil.CreateDelta<EntityDelta>(typeof(RS_Table));
                    delta.Conditions.Add("TableId", Table.TableId);
                    delta.DeltaProperties.Add("TableNo", Table.TableNo);
                    delta.DeltaProperties.Add("SeatCount", Table.SeatCount);

                    Aide.LocalClient.Invoke<int>(
                        service => service.SetTable(delta));
                    break;
                default:
                    break;
            }

            base.AddData();
        }
    }
}
